traj <- fread("../1_data/example_mobility_trajectory.csv")
events <- fread("../1_data/example_life_events.csv")
# traj; events
setorder(traj, end_date)
traj[ , end_year := year(end_date), ]
city_order <- traj$city
city_order_dt <- data.table(from = traj$city, to = c(traj$city[-1], "groningen"))
traj <- traj %>% rename(from = city) %>% left_join(city_order_dt, by = "from")
timeline <- data.table(end_year = min(traj$end_year):max(traj$end_year))
timeline <- timeline %>% left_join(traj %>% select(affiliation, end_year, from, to), by = "end_year") %>%
mutate(last_city = paste0("move_to: ", from)) %>%
mutate(
affiliation = na.locf(affiliation, fromLast = TRUE),
from = na.locf(from, fromLast = TRUE),
to = na.locf(to, fromLast = TRUE))
unique_locations <- traj %>% select(from, latitude, longitude) %>% unique() %>% st_as_sf(coords = c("longitude", "latitude"), crs = 4326)
sf::sf_use_s2(T)
geo_dist_matrix <- st_distance(unique_locations, by_element = F)
geo_dist_dt <- as.data.table(geo_dist_matrix)
setnames(geo_dist_dt, old = 1:ncol(geo_dist_dt), unique_locations$from)
geo_dist_dt[ , from := unique_locations$from]
geo_dist_dt <- geo_dist_dt %>% pivot_longer(cols = unique_locations$from, names_to = "to", values_to = "dist")
geo_dist_dt <- geo_dist_dt %>% mutate(dist_km = round(as.numeric(dist)/1000, 2)) %>% select(-dist) %>% setDT()
geo_dist_dt
## from to dist_km
## 1: amsterdam amsterdam 0.00
## 2: amsterdam utrecht 34.70
## 3: amsterdam paris 429.96
## 4: amsterdam groningen 146.11
## 5: utrecht amsterdam 34.70
## 6: utrecht utrecht 0.00
## 7: utrecht paris 408.42
## 8: utrecht groningen 159.41
## 9: paris amsterdam 429.96
## 10: paris utrecht 408.42
## 11: paris paris 0.00
## 12: paris groningen 566.70
## 13: groningen amsterdam 146.11
## 14: groningen utrecht 159.41
## 15: groningen paris 566.70
## 16: groningen groningen 0.00
timeline <- timeline %>% left_join(geo_dist_dt, by = c("from", "to"))
timeline <- timeline %>% mutate(cum_dist_km = cumsum(dist_km))
pp <- plot_ly(data = timeline, x = ~end_year, y = ~cum_dist_km, z = ~ from, type = 'scatter3d', mode = 'lines') %>%
add_markers() %>%
layout(title = '\nMobility trajectory', scene = list(xaxis=list(title = 'Year'),yaxis=list(title = 'Total distnace travelled'),zaxis=list(title="City")))
pp